/*
 * 文 件 名 : PopupEditViewWindow.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-8-20
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui;


import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.fire8.dolphin.db.AnswerDAO;
import com.fire8.dolphin.utils.StringUtil;

/**
 * <一句话功能简介><br>
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2012-8-20]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class PopupEditViewWindow extends PopupWindow
{
    private final String TAG="dolphin";
    private static PopupEditViewWindow pw;//弹出窗口
    private LinearLayout mLayout;//布局，里面放一个文档框和一个按钮
    AnswerDAO answerDao;
    OnClickListener cnClickListener;
    String locId;
    private PopupEditViewWindow(){}
    public static PopupEditViewWindow getInstall(Context c,OnClickListener cnClickListener,String lId)
    {
        if(pw==null)
        {
            pw = new PopupEditViewWindow(c,cnClickListener,lId);
        }
        return pw;
    }
    private PopupEditViewWindow(Context c,OnClickListener cnClickListener,String lId)
    {
        super(c);
        pw = this;
        initLayout(c,cnClickListener);
        answerDao = new AnswerDAO(c);
        locId = lId;
        //LayoutInflater factory = LayoutInflater.from(c);  //加载popWindow的layout  
        //final View buttonView = factory.inflate(R.layout.answer_item, null); 
        //pw = new PopupWindow(buttonView,400,400); //请注意，使用此构造方法，一定要指定popupWindow的长宽，之后调用popupWindow的showAXXX方法才会显示指定的contentView
        ColorDrawable cd = new ColorDrawable(-0000);
        this.setBackgroundDrawable(cd);
        this.setFocusable(true);
        this.setTouchInterceptor(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_OUTSIDE)
                {
                    Log.d(TAG,"按到外面了，关闭窗口");
                    pw.dismiss();
                    return true;
                }
                return false;
            }
        });
        
        //设置默认项  
        this.setContentView(mLayout);  
        this.setWidth(LayoutParams.FILL_PARENT);  
        this.setHeight(LayoutParams.WRAP_CONTENT);  
        //this.setBackgroundDrawable(new ColorDrawable());// 设置窗口背景  
        //this.setAnimationStyle(aniTabMenu);  
        this.setFocusable(true);// 窗口获得焦点 如果没有获得焦点窗口中的控件事件无法响应  
    }
    
    private void initLayout(final Context c,OnClickListener cnClickListener)
    {
        mLayout = new LinearLayout(c);  
        mLayout.setOrientation(LinearLayout.VERTICAL);
        final EditText edit = new EditText(c);
        edit.setTextSize(24);
        edit.setLines(5);
        edit.setGravity(Gravity.TOP);
        edit.setWidth(LayoutParams.FILL_PARENT);
        edit.setHeight(LayoutParams.WRAP_CONTENT);
        mLayout.addView(edit);
        Button btnSend = new Button(c);
        btnSend.setWidth(LayoutParams.FILL_PARENT);
        btnSend.setHeight(LayoutParams.WRAP_CONTENT);
        btnSend.setText("发送");
        btnSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"提交回复");
                String strAnswer = edit.getText().toString();
                if(StringUtil.isEmpty(strAnswer))
                {
                    Toast.makeText(c, "请输入回答内容", Toast.LENGTH_LONG).show();
                    return;
                }
                
            }
        });
        mLayout.addView(btnSend);
    }
    
    
}
